Kotlin

Collection Aggregate Operations

Swift
                  val numbers = listOf(6, 42, 10, 4)
​
println("Count: ${numbers.count()}")
println("Max: ${numbers.maxOrNull()}")
println("Min: ${numbers.minOrNull()}")
println("Average: ${numbers.average()}")
println("Sum: ${numbers.sum()}")
                
                    let numbers = listOf(6, 42, 10, 4)
​
print("Count: ${numbers.count()}")
print("Max: ${numbers.maxOrNull()}")
print("Min: ${numbers.minOrNull()}")
print("Average: ${numbers.average()}")
print("Sum: ${numbers.sum()}")
                  
                  val numbers = listOf(5, 42, 10, 4)
val min3Remainder = numbers.minByOrNull { it % 3 }
println(min3Remainder)
​
val strings = listOf("one", "two", "three", "four")
val longestString = strings.maxWithOrNull(compareBy { it.length })
println(longestString)
                
                    let numbers = listOf(5, 42, 10, 4)
let min3Remainder = numbers.minByOrNull { $0 % 3 }
print(min3Remainder)
​
let strings = listOf("one", "two", "three", "four")
let longestString = strings.maxWithOrNull(compareBy { it.length })
print(longestString)
                  
                  ​
val numbers = listOf(5, 42, 10, 4)
println(numbers.sumBy { it * 2 })
println(numbers.sumByDouble { it.toDouble() / 2 })
                
                    ​
let numbers = listOf(5, 42, 10, 4)
print(numbers.sumBy { $0 * 2 })
print(numbers.sumByDouble { it.toDouble() / 2 })
                  

Fold and reduce

                  val numbers = listOf(5, 2, 10, 4)
​
val sum = numbers.reduce { sum, element -> sum + element }
println(sum)
val sumDoubled = numbers.fold(0) { sum, element -> sum + element * 2 }
println(sumDoubled)
​
//val sumDoubledReduce = numbers.reduce { sum, element -> sum + element * 2 } //incorrect: the first element isn't doubled in the result
//println(sumDoubledReduce)
                
                    let numbers = listOf(5, 2, 10, 4)
​
let sum = numbers.reduce { sum, element -> sum + element }
print(sum)
let sumDoubled = numbers.fold(0) { sum, element -> sum + element * 2 }
print(sumDoubled)
​
//let sumDoubledReduce = numbers.reduce { sum, element -> sum + element * 2 } //incorrect: the first element isn't doubled in the result
//print(sumDoubledReduce)
                  
                  val numbers = listOf(5, 2, 10, 4)
val sumDoubledRight = numbers.foldRight(0) { element, sum -> sum + element * 2 }
println(sumDoubledRight)
                
                    let numbers = listOf(5, 2, 10, 4)
let sumDoubledRight = numbers.foldRight(0) { element, sum -> sum + element * 2 }
print(sumDoubledRight)
                  
                  val numbers = listOf(5, 2, 10, 4)
val sumEven = numbers.foldIndexed(0) { idx, sum, element -> if (idx % 2 == 0) sum + element else sum }
println(sumEven)
​
val sumEvenRight = numbers.foldRightIndexed(0) { idx, element, sum -> if (idx % 2 == 0) sum + element else sum }
println(sumEvenRight)
                
                    let numbers = listOf(5, 2, 10, 4)
let sumEven = numbers.foldIndexed(0) { idx, sum, element -> if (idx % 2 == 0) sum + element else sum }
print(sumEven)
​
let sumEvenRight = numbers.foldRightIndexed(0) { idx, element, sum -> if (idx % 2 == 0) sum + element else sum }
print(sumEvenRight)